package com.aiflow.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.aiflow.db.ConnDB;

public class Proving {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	
	/*
	 * 用于验证 是否重复
	 */
	public String provingOnly(){
	   HttpServletRequest  req = ServletActionContext.getRequest();
	   String val = req.getParameter("val");
	   String filed = req.getParameter("filed");
	   String table = req.getParameter("table");
	   String sql = " select * from "+table+" where "+filed +"= '"+val+"'";
	   ConnDB db = new ConnDB();
		Connection con = db.getConnection();
	    Statement ps = null;
	    ResultSet rs = null;
	    int count =0;
	    try {
	    	ps = con.createStatement();
			rs = ps.executeQuery(sql);
			
			while(rs.next()){
				count = rs.getInt(1);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	   HttpServletResponse  res = ServletActionContext.getResponse();
	   res.setCharacterEncoding("utf-8");
	   try {
		PrintWriter wri = res.getWriter();
		wri.print(String.valueOf(count));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return null;
	}
	
	public void proving(){
		   HttpServletRequest  req = ServletActionContext.getRequest();
		   String val = req.getParameter("val");
		   String filed = req.getParameter("filed");
		   String table = req.getParameter("table");
		   String sql = " select * from "+table+" where "+filed +"= '"+val+"'";
		   ConnDB db = new ConnDB();
			Connection con = db.getConnection();
		    Statement ps = null;
		    ResultSet rs = null;
		    JSONObject json = new JSONObject();  
		    try {
		    	ps = con.createStatement();
				rs = ps.executeQuery(sql);
				while(rs.next()){
					json.put("success", true);
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		   HttpServletResponse  res = ServletActionContext.getResponse();
		   res.setCharacterEncoding("utf-8");
		   try {
			PrintWriter wri = res.getWriter();
			wri.print(json.toString());
			wri.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
}
